package com.sanji.pay.allinpay.service;

import com.sanji.pay.allinpay.entity.AllinPay;
import com.sanji.pay.allinpay.entity.Refund;
import com.sanji.pay.allinpay.repository.RefundRepository;
import com.sanji.pay.allinpay.util.SybPayService;
import com.sanji.pay.common.utils.DateUtil;
import com.sanji.pay.common.utils.PayNumUtil;
import com.sanji.pay.pay.entity.PayOrder;
import com.sanji.pay.pay.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 描述: 退款业务层
 * 包名: com.sanji.pay.allinpay.service
 * 作者: ChenGuop.
 * 日期: 2017/09/13 上午10:34.
 * 项目名称: sanji-pay
 * 版本: 1.0
 * JDK: since 1.8
 */
@Service
public class RefundService {
  @Autowired
  private RefundRepository refundRepository;
  @Autowired
  private AllinPayService allinPayService;
  @Autowired
  private SybPayService sybPayService;
  @Autowired
  private PayOrderService payOrderService;

  /**
   * 撤销
   * @param payNumber
   * @return
   */
  public Refund cancel(String payNumber){
    AllinPay allinPay = allinPayService.findByPayNum(payNumber);
    if (allinPay == null||allinPay.getTrxStatus()!="0000"){
      return null;
    }
    //生成支付单号
    String payNum = PayNumUtil.setPayNum();
    Refund refund = new Refund();
    try {
      Map<String,String> map = sybPayService.cancel(allinPay.getTrxAmt(),payNum,allinPay.getTrxId(),allinPay.getPayNum());
      if (map.containsKey("trxstatus")&&"0000".equals(map.get("trxstatus"))) {
        refund.setAllinPay(allinPay);
        refund.setTrxAmt(allinPay.getTrxAmt());
        refund.setRefundNumber(map.get("reqsn"));
        refund.setTrxId(map.get("trxid"));
        refund.setType("cancel"); //撤销
        refund.setFinishTime(DateUtil.getDateStrToDate(map.get("fintime"), "yyyyMMddHHmmss"));
        refund.setStatus(map.get("trxstatus"));
        refund.setErrorMsg(map.get("errmsg"));
        refund.setRandomStr(map.get("randomstr"));
        refund.setSign(map.get("sign"));

        //修改支付订单状态
        PayOrder payOrder = payOrderService.findByPayNumber(payNumber);
        payOrder.setStatus(PayOrder.PayStatus.CANCEL);
        payOrderService.save(payOrder);
        return refundRepository.save(refund);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  public Refund refund(String payNumber, Integer amount){
    AllinPay allinPay = allinPayService.findByPayNum(payNumber);
    if (allinPay == null||allinPay.getTrxStatus()!="0000"){
      return null;
    }
    //生成支付单号
    String payNum = PayNumUtil.setPayNum();
    Refund refund = new Refund();
    try {
      Map<String,String> map = sybPayService.refund(amount,payNum,allinPay.getTrxId(),allinPay.getPayNum());
      if (map.containsKey("trxstatus")&&"0000".equals(map.get("trxstatus"))) {
        refund.setAllinPay(allinPay);
        refund.setTrxAmt(allinPay.getTrxAmt());
        refund.setRefundNumber(map.get("reqsn"));
        refund.setTrxId(map.get("trxid"));
        refund.setType("refund"); //退款
        refund.setFinishTime(DateUtil.getDateStrToDate(map.get("fintime"), "yyyyMMddHHmmss"));
        refund.setStatus(map.get("trxstatus"));
        refund.setErrorMsg(map.get("errmsg"));
        refund.setRandomStr(map.get("randomstr"));
        refund.setSign(map.get("sign"));

        //修改支付订单状态
        PayOrder payOrder = payOrderService.findByPayNumber(payNumber);
        payOrder.setStatus(PayOrder.PayStatus.REFUND);
        payOrderService.save(payOrder);
        return refundRepository.save(refund);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

}
